home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / LOGMIN_F.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  15KB  |  440 lines

  1. /****************************************************************************
  2.  *
  3.  *      Program name : LOGMIN_F.C
  4.  *
  5.  *    Written By : Eng-Huat Ong and Kian-Mong Low.
  6.  *
  7.  *      This is the actual minimization algorithm. Variation from the actual
  8.  *    LOGMIN algorithm is as follows:
  9.  *        1.    Minterms with adjacency 0 or 1 is minimised first
  10.  *        2.    Select next minterm with lowest adjacency wrt b-array
  11.  *        3.  If more than 1 such minterms, select one with the lowest
  12.  *            adjacency wrt to a-array
  13.  *        4.    To shrink the CPT, select an adjacent term, mi that has the
  14.  *            largest wi AND is already covered
  15.  *        5.  If more than one such adjacent terms are covered, select one
  16.  *        with the lowest adjacency wrt to b-array
  17.  *
  18.  * --------------------------------------------------------------------------
  19.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  20.  *    University.
  21.  *
  22.  *    You are free to use, copy and distribute this software and its
  23.  *    documentation providing that:
  24.  *
  25.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  26.  *
  27.  *        IT IS NOT MODIFIED IN ANY WAY.
  28.  *
  29.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  30.  *
  31.  *    This program is provided "AS IS" without any warranty, expressed or
  32.  *    implied, including but not limited to fitness for any particular
  33.  *    purpose.
  34.  *
  35.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  36.  *    appreciated. Please send to:
  37.  *
  38.  *        Boon-Tiong Tan or Othman Bin Ahmad
  39.  *        School of EEE
  40.  *        Nanyang Technological University
  41.  *        Nanyang Avenue
  42.  *        Singapore 2263
  43.  *        Republic of Singapore
  44.  *
  45.  ***************************************************************************/
  46.  
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #define mask8 255
  51.  
  52. unsigned char   *logmin_f(a, b)              /* pointer to a & b array passed */
  53. unsigned char   *a, *b;
  54.  
  55. {
  56.    unsigned short   pterm, ma, mb, *pm, *pm1, pos, pos1;
  57.    unsigned  long   m, k, count, count1, topow();
  58.    register  long   i, wo, wi;
  59.    register  char   j;
  60.          char   test;
  61.    unsigned  char   *c, *d, *e, *s, *temp, adj0, adji;
  62.    unsigned  char   n, adj, adjacency(), nspm, cover;
  63.    unsigned  char   *adjacent(), *reduce(), *cpt(), *ssm(), adj_of_mi();
  64.  
  65.  
  66.    mb = *(b+2)<<8 | *(b+1);            /* no. of minterms in b-array */
  67.  
  68.    n    = *a;                          /* no. of variables */
  69.    nspm = *(a+3);                      /* no. of bytes/minterm */
  70.    ma = *(a+2)<<8 | *(a+1);            /* no. of minterms in a-array */
  71.  
  72.    temp = (unsigned char *) malloc(nspm+1);        /* temporary storage */
  73.    if (temp == 0)
  74.       {
  75.      printf("Out of memory -- LOGMIN_F, *temp\n");
  76.      printf("Program terminated - 1\n");
  77.      exit(0);
  78.       }
  79.  
  80.    s = (unsigned char *) malloc(4);                /* header of s-array */
  81.    if (s == 0)
  82.       {
  83.      printf("Out of memory -- LOGMIN_F, *s\n");
  84.      printf("Program terminated - 2\n");
  85.      exit(0);
  86.       }
  87.  
  88.    pterm = 0;                                /* no. of product term */
  89.  
  90.    /***
  91.     ***  MINIMIZE ALL MINTERMS WITH ADJACENCY OF 0 & 1 FIRST
  92.     ***/
  93.  
  94.    for (i=0; i<mb; i++)                      /* for adj = 0 or 1 */
  95.       {
  96.      *b = n;                             /* assign back to n */
  97.  
  98.      memcpy(temp, (b+4+nspm*i), nspm);   /* pick a minterm from b-array */
  99.      c = adjacent(temp, a, 1);           /* obtain the adjacent terms */
  100.      adj = *(c+1);                       /* adjacency of minterm */
  101.  
  102.      if (adj <= 1)                       /* adjacency 0 or 1 */
  103.         {
  104.            d = cpt(c);                   /* generate CPT */
  105.  
  106.            s = (unsigned char *) realloc(s,4+n*(pterm+1));   /* more space */
  107.            if (s == 0)
  108.           {
  109.              printf("Out of memory -- LOGMIN_F, *s\n");
  110.              printf("Program terminated - 3\n");
  111.              exit(0);
  112.           }
  113.            memcpy ((s+4+n*pterm),d,n);   /* add CPT to soln array */
  114.            pterm++;                      /* product term count */
  115.  
  116.            b = reduce(c,b,i);            /* remove minterms covered from b-array */
  117.            i = (char) *b;                /* index pointer of b-array */
  118.            mb = *(b+2)<<8 | *(b+1);      /* value of mb altered in b-array */
  119.  
  120.            free(d);                    /* free pointer */
  121.         }
  122.      free(c);                            /* free pointer */
  123.       }
  124.  
  125.     /***
  126.      ***  MINIMIZE MINTERMS WITH ADJACENCY GREATER THAN 1
  127.      ***/
  128.  
  129.     while (mb > 0)
  130.        {
  131.       adj0 = 255;                     /* max for 8-bit */
  132.  
  133.       *b = n;                         /* assign back to n */
  134.  
  135.       /***
  136.        ***  OBTAIN AN ARRAY, PM OF POSITION OF ADJACENT TERMS WITH THE LOWEST
  137.        ***  ADJACENCY WRT B-ARRAY
  138.        ***/
  139.  
  140.       for (i=0; i<mb; i++)
  141.          {
  142.         memcpy(temp, (b+4+nspm*i), nspm);    /* pick a minterm */
  143.         adji = adjacency(temp, b);           /* adj wrt b-array */
  144.  
  145.         if (adji<adj0)                       /* look for lowest adj */
  146.            {
  147.               if (adj0!=255)                 /* not the 1st term */
  148.              free(pm);
  149.  
  150.               adj0 = adji;                   /* update lowest adj */
  151.               count = 1;                        /* set count to 1 */
  152.               pm = (unsigned short *) malloc(2);   /* space for pos */
  153.               if (pm == 0)
  154.              {
  155.                 printf("Out of memory -- LOGMIN_F, *pm\n");
  156.                 printf("Program terminated - 4\n");
  157.                 exit(0);
  158.              }
  159.               *pm = i;                       /* 1st pos of minterm */
  160.            }
  161.  
  162.         else if (adji==adj0)                 /* adjacency as low */
  163.            {
  164.               count++;                       /* no. of elements in pm-array */
  165.               pm = (unsigned short*) realloc(pm, 2*count);    /* more space */
  166.               if (pm == 0)
  167.              {
  168.                 printf("Out of memory -- LOGMIN_F, *pm\n");
  169.                 printf("Program terminated - 5\n");
  170.                 exit(0);
  171.              }
  172.               *(pm+count-1) = i;             /* elements in pm-array */
  173.            }
  174.          }
  175.  
  176.       if (count==1)                  /* if only 1 element in pm-array */
  177.          pos = *pm;
  178.       else
  179.          {
  180.         /***
  181.          ***  FORM PM-ARRAY, SELECT THE 1ST ADJACENT TERM THAT HAS THE
  182.          ***  LOWEST ADJACENCY WRT A-ARRAY
  183.          ***/
  184.  
  185.         adj0 = 255;                      /* max for 8-bit */
  186.         for (i=0; i<count; i++)          /* do for all in pm-array */
  187.            {
  188.               pos1 = *(pm+i);                     /* pos of minterm */
  189.               memcpy(temp, (b+4+nspm*pos1), nspm);
  190.               adji = adjacency(temp, a);          /* adj wrt a-array */
  191.  
  192.               if (adji<adj0)                      /* new lowest found */
  193.              {
  194.                 adj0 = adji;                  /* update lowest */
  195.                 pos = *(pm+i);                /* position of required minterm */
  196.              }
  197.            }
  198.          }
  199.        free(pm);
  200.  
  201.        memcpy(temp, (b+4+nspm*pos), nspm);       /* pick the required minterm */
  202.  
  203.        /***
  204.         ***  GENERATE SSM AND TEST FOR COVERAGE BY FUNCTION
  205.         ***/
  206.  
  207.        c = adjacent(temp, a, 255);          /* obtain adjacent terms */
  208.        adj = *(c+1);                        /* adjacency of minterms */
  209.        d = cpt(c);                          /* generate CPT */
  210.        e = ssm(d);                          /* generate SSM */
  211.        m = topow(*(e+1));                   /* no. of SSM terms */
  212.  
  213.        for (j=0; j<m; j++)                  /* check for SSM coverage */
  214.           {
  215.          memcpy (temp, (e+4+nspm*j), nspm);
  216.          test = exist (temp, a, ma);
  217.          if (test == -1)                /* minterm not in a-array */
  218.              break;
  219.           }
  220.  
  221.        /***
  222.         ***  ALL SSM COVERED BY THE FUNCTION, SELECT CPT AS PRODUCT TERM
  223.         ***/
  224.  
  225.        if (test == 0)                       /* all SSM's covered by fn */
  226.           {
  227.          if (m > 65536)                 /* too many SSM terms */
  228.             {
  229.                printf("Product Term Too Huge \n");
  230.                printf("Program terminated \n");
  231.                exit(0);
  232.             }
  233.          *(e+1) = (m-1) & mask8;        /* no. of SSM terms minus 1 */
  234.          *(e+2) = (m-1)>>8 & mask8;
  235.          b = reduce(e,b,i);             /* remove minterms covered from b-array */
  236.          mb = *(b+2)<<8 | *(b+1);       /* no. of minterms in b-array */
  237.  
  238.          s = (unsigned char *) realloc(s, 4+n*(pterm+1)); /* more space */
  239.          if (s == 0)
  240.             {
  241.                printf("Out of memory -- LOGMIN_F, *s\n");
  242.                printf("Program terminated - 6\n");
  243.                exit(0);
  244.             }
  245.          memcpy((s+4+n*pterm),d,n);     /* add CPT to soln array */
  246.          pterm++;                       /* no. of product terms */
  247.  
  248.          free(d);                       /* free pointers */
  249.          free(e);
  250.           }
  251.        else
  252.           {
  253.          /***
  254.           ***  SSM NOT COVERED BY THE FUNCTION
  255.           ***/
  256.  
  257.          free(d);                       /* free pointer */
  258.          cover =0;                      /* coverage status */
  259.  
  260.          while (!cover)        /* do until shrinked SSM is covered */
  261.             {
  262.                /***
  263.             ***  OBTAIN AN ARRAY, PM OF POSITIONS OF ADJACENT TERMS, MI WITH THE LARGEST WI
  264.             ***/
  265.  
  266.                wo = -1;                        /* initial value */
  267.                for (j=0; j<adj; j++)           /* do for all adjacent terms */
  268.               {
  269.                  memcpy(temp,(c+4+nspm*(j+1)),nspm); /* pick adjacent term */
  270.  
  271.                  wi = adj_of_mi(temp,e,a);           /* obtain wi */
  272.                  if (wi>wo)
  273.                 {
  274.                    if (wo != -1)                 /* not the first */
  275.                       free(pm);
  276.                    wo = wi;                      /* new lowest value */
  277.                    count = 1;                    /* reset count to 1 */
  278.  
  279.                    pm = (unsigned short *) malloc(2);  /* space for pm */
  280.                    if (pm==0)
  281.                       {
  282.                      printf("Out of memory -- LOGMIN_F, *pm\n");
  283.                      printf("Program terminated - 7\n");
  284.                      exit(0);
  285.                       }
  286.                    *pm = j;                      /* first element */
  287.                 }
  288.                  else if (wi==wo)                    /* wi as large */
  289.                 {
  290.                    count++;                      /* no. of element in pm-array */
  291.  
  292.                    pm = (unsigned short *) realloc (pm, 2*count);  /* more space */
  293.                    if (pm==0)
  294.                       {
  295.                      printf("Out of memory -- LOGMIN_F, *pm\n");
  296.                      printf("Program terminated - 8\n");
  297.                      exit(0);
  298.                       }
  299.                    *(pm+count-1) = j;            /* elements of pm-array */
  300.                 }
  301.               }
  302.                free(e);                                  /* free pointer */
  303.  
  304.                /***
  305.             ***  DETERMINE FROM PM-ARRAY, AN ARRAY, PM1 OF POSITION OF ADJACENT TERMS
  306.             ***  THAT HAVE ALREADY BEEN COVERED
  307.             ***/
  308.  
  309.                count1 = count;                           /* no. of elements in pm & pm1 */
  310.  
  311.                pm1 = (unsigned short *) malloc(2);       /* space for pm1 */
  312.                if (pm1==0)
  313.               {
  314.                  printf("Out of memory -- LOGMIN_F, *pm1\n");
  315.                  printf("Program terminated - 9\n");
  316.                  exit(0);
  317.               }
  318.  
  319.                k = 0;
  320.                for (j=0; j<count; j++)              /* do for all element in pm-array */
  321.               {
  322.                  memcpy(temp, (c+4+nspm*(1+ *(pm+j))), nspm);    /* pick adj term */
  323.                  test = exist(temp,b,mb);
  324.                  if (test == -1)                               /* already covered */
  325.                 {
  326.                    memcpy((pm1+k++), (pm+j), 2);       /* transfer pos to pm1 */
  327.                    count1--;                                       /* reduced */
  328.  
  329.                    pm1 = (unsigned short *) realloc(pm1, 2*(k+1)); /*  more space */
  330.                    if (pm1==0)
  331.                       {
  332.                      printf("Out of memory -- LOGMIN_F, *pm1\n");
  333.                      printf("Program terminated - 10\n");
  334.                      exit(0);
  335.                       }
  336.                 }
  337.               }
  338.  
  339.                /***
  340.             ***  SELECT FROM PM1-ARRAY, THE 1ST ADJACENT TERM, MI WITH THE LOWEST
  341.             ***  ADJACENCY WRT B-ARRAY
  342.             ***/
  343.  
  344.                adj0 = 255;                     /* max of 8-bit */
  345.                for (j=0; j<k; j++)             /* do for all in pm1-array */
  346.               {
  347.                  memcpy(temp,(c+4+nspm*(1+*(pm1+j))),nspm);  /* pick an adj term */
  348.                  adji = adjacency(temp,b);                   /* obtain adjacency */
  349.  
  350.                  if (adji<adj0)                        /* new lowest found */
  351.                 {
  352.                    adj0 = adji;                    /* update lowest */
  353.                    pos = *(pm1+j);                 /* position of lowest adj term */
  354.                 }
  355.               }
  356.  
  357.                if (count1==count)                /* select the 1st if all not covered */
  358.               pos = *pm;
  359.  
  360.                adj--;                            /* no. of adj terms in c-array */
  361.                *(c+1) = adj;                     /* adjacency after shrinking CPT */
  362.  
  363.                free(pm);                         /* free pointer */
  364.                free(pm1);
  365.  
  366.                /***
  367.             ***  SHRINK CPT (REMOVE 1 ADJACENT TERM FROM C-ARRAY), GENERATE NEW CPT, SSM
  368.             ***  AND TEST FOR COVERAGE BY FUNCTION
  369.             ***/
  370.  
  371.                memcpy((c+4+nspm*(1+pos)), (c+4+nspm*(2+pos)), nspm*(adj-pos));  /* shrink CPT */
  372.  
  373.                c = (unsigned char*) realloc(c, 4+nspm*(1+adj));   /* reduce space */
  374.                if (c==0)
  375.               {
  376.                  printf("Out of memory -- LOGMIN_F, *c\n");
  377.                  printf("Program terminated - 11\n");
  378.                  exit(0);
  379.               }
  380.  
  381.                d = cpt(c);                  /* generate CPT */
  382.                e = ssm(d);                  /* generate SSM */
  383.                m = topow(*(e+1));           /* no. of SSM terms */
  384.  
  385.                for (k=0; k<m; k++)          /* check SSM coverage by function */
  386.               {
  387.                  memcpy(temp, (e+4+nspm*k), nspm);  /* pick SSM term */
  388.                  test = exist(temp,a,ma);
  389.                  if (test == -1)                    /* SSM term not covered */
  390.                 break;
  391.               }
  392.  
  393.                /***
  394.             ***  SHRINKED SSM COVERED BY FUNCTION, REDUCE B-ARRAY
  395.             ***  AND SELECT SHRINKED CPT AS PRODUCT TERM
  396.             ***/
  397.  
  398.                if (test==0)                   /* SSM covered */
  399.               {
  400.                  if (m > 65536)           /* too many SSM terms */
  401.                 {
  402.                    printf("Product Term Too Huge \n");
  403.                    printf("Program terminated \n");
  404.                    exit(0);
  405.                 }
  406.                  *(e+1) = (m-1) & mask8;  /* no. of SSM terms minus 1 */
  407.                  *(e+2) = (m-1)>>8 & mask8;
  408.                  b = reduce(e, b, i);     /* remove minterms covered from b-array */
  409.                  mb = *(b+2)<<8 | *(b+1); /* no. of minterms in b-array */
  410.  
  411.                  s = (unsigned char *) realloc(s, 4+n*(pterm+1));  /* more space */
  412.                  if (s==0)
  413.                 {
  414.                    printf("Out of memory -- LOGMIN_F, *s\n");
  415.                    printf("Program terminated - 12\n");
  416.                    exit(0);
  417.                 }
  418.                  memcpy ((s+4+n*pterm), d, n);      /* add shrinked CPT to soln array */
  419.                  pterm++;                           /* no. of product terms */
  420.  
  421.                  cover = 1;                         /* coverage status */
  422.                  free(e);                           /* free pointer */
  423.               }
  424.                free (d);                      /* free pointer */
  425.             }
  426.           }
  427.       free(c);                   /* free pointer */
  428.       }
  429.    *s = n;                           /* no. of variables */
  430.    *(s+1) = pterm & mask8;           /* no. of product terms in 2 bytes */
  431.    *(s+2) = pterm>>8 & mask8;
  432.  
  433.    free(temp);                       /* free pointer */
  434.    free(a);
  435.    free(b);
  436.  
  437.    return(s);                        /* return solution array */
  438. }
  439.  
  440.